from django.contrib.auth import authenticate, login
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.contrib.sitemaps import Sitemap
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from fblaproj.accounts.models import UserProfile
from django.forms.fields import email_re
from django import forms
import random 
import sha 
def success(request):
	return render_to_response('register/success.html', context_instance = RequestContext(request))
def register(request):
	if (request.user.is_authenticated()):
		return HttpResponseRedirect('http://fblaconnection.com/')
	if request.POST:
		f_name = request.POST['first_name']
		l_name = request.POST['last_name']
		username = request.POST['username']
		email = request.POST['email']
		c_email = request.POST['confirm_email']
		grade = request.POST['grade']
		password = request.POST['password']
		c_password = request.POST['confirm_password']
		grade = request.POST['grade']
		errors = None
		fblank_error = None
		password_error  = None
		email_error = None	
		email_valid_error = None
		email_mult = True
		username_mult = True
		context = {
			'f_name' : f_name,
			'l_name' : l_name,
			'username' : username,
			'email' : email,
			'c_email' : c_email,
			'grade' : grade,
			'password' : password,
			'c_password' : c_password,
			'grade' : grade,
		}
		try:
			user = User.objects.get(email=email)
		except User.DoesNotExist:
			email_mult = False
		try:
			user = User.objects.get(username=username)
		except User.DoesNotExist:
			username_mult = False

		if (email_mult == True):
			errors = True
		if (username_mult == True):
			errors = True
		if (f_name == "" or l_name == "" or username == "" or 
			email == "" or c_email == "" or grade == "" or 
			password == "" or c_password == ""):
				errors = True
				fblank_error = True
		if (password != c_password):
			errors = True
			password_error = True
		if not email_re.match(email):
			errors = True
			email_valid_error = True
		if (email != c_email):
			errors = True
			email_error = True
		context.update({
			'errors' : errors,
			'fblank_error' : fblank_error,
			'password_error' : password_error,
			'email_error' : email_error,
			'email_valid_error' :email_valid_error,
			'email_mult' : email_mult,
			'username_mult' : username_mult,
		})
		if errors:
			return render_to_response('register/register.html', context, context_instance = RequestContext(request))
		else:
			rand = str(random.random()) 
			salt = sha.new(rand).hexdigest()[:5]
			hsh = sha.new(salt+password).hexdigest() 
			password_hashed = 'sha1$' + salt + '$' + hsh
			user = User(username=username, password=password_hashed, email=email, first_name=f_name, last_name=l_name)
			user.is_staff = False
			user.is_superuser = False
			user.save()
			profile = UserProfile(grade=grade, user_id=user.id)
			profile.save() 
			return HttpResponseRedirect('http://fblaconnection.com/register/success/')
	else:
		context = {
		
		}
		return render_to_response('register/register.html', context, context_instance = RequestContext(request))